package com.ocm.tourism.core.dao.jpa;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.ocm.tourism.core.dao.CataloguesDao;
import com.ocm.tourism.core.domain.Catalogues;
import com.ocm.tourism.core.domain.CataloguesFileInfo;
import com.ocm.tourism.core.domain.SightSeeing;

@Repository("cataloguesDao")
public class JpaCataloguesDao extends JpaGenericDao<Catalogues> implements CataloguesDao {


   public JpaCataloguesDao() {
      this.setClazz(Catalogues.class);
   }

   public long count() {
      return entityManager().createQuery("SELECT COUNT(o) FROM Catalogues o", Long.class).getSingleResult();
   }

   public List<Catalogues> findAll() {
      return entityManager().createQuery("SELECT o FROM Catalogues o", Catalogues.class).getResultList();
   }

   @SuppressWarnings("unchecked")
   public List<Catalogues> findAll(List<Long> catalogueIds) {
      Query query = this.entityManager().createQuery("SELECT c FROM Catalogues c WHERE c.id IN (:catalogues)", Catalogues.class);
      query.setParameter("catalogues", catalogueIds);
      List<Catalogues> catalogues = query.getResultList();
      return catalogues;
   }
   
   @SuppressWarnings("unchecked")
   public Catalogues find(String newType) {
      if (newType == null)
         return null;
      Query query = entityManager().createQuery("SELECT o FROM Catalogues o WHERE o.cataloguesType =:type",
            Catalogues.class);
      query.setParameter("type", newType);
      
      List<Catalogues> catalogues = (List<Catalogues>) query.getResultList();
      if (catalogues.size() > 0)
         return catalogues.get(0);
      return null;
   }

   public List<Catalogues> findEntries(int firstResult, int maxResults) {
      return entityManager().createQuery("SELECT o FROM Catalogues o", Catalogues.class).setFirstResult(firstResult)
            .setMaxResults(maxResults).getResultList();
   }

   @SuppressWarnings("unchecked")
   public List<CataloguesFileInfo> findCataloguesFileInfos(int firstResult, int maxResults,String cataloguesType) {
      if (cataloguesType == null)
         return null;
      Query query = entityManager().createQuery("SELECT o FROM CataloguesFileInfo o WHERE o.cataloguesType =:type",
            Catalogues.class).setFirstResult(firstResult).setMaxResults(maxResults);
      query.setParameter("type", cataloguesType);
      
      List<CataloguesFileInfo> catalogues = (List<CataloguesFileInfo>) query.getResultList();
      return catalogues;
   }

   @Override
   public long countCataloguesFileInfos(String cataloguesType) {
      Query query = entityManager().createQuery("SELECT COUNT(o) FROM CataloguesFileInfo o WHERE o.cataloguesType =:type",
            Long.class);
      query.setParameter("type", cataloguesType);
      long count = (Long) query.getSingleResult();
      return count;
   }

   @Override
   public CataloguesFileInfo findCataloguesFileInfo(long id) {
      CataloguesFileInfo cataloguesFileInfo = entityManager().find(CataloguesFileInfo.class, id);
      return cataloguesFileInfo;
   }

   @Override
   public SightSeeing findSightSeeing(long id) {
      SightSeeing sightSeeing = entityManager().find(SightSeeing.class, id);
      return sightSeeing;
   }
}
